﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Text.RegularExpressions;

namespace Frame.Lib.Web
{
    public sealed class HtmlPager
    {

        public static string Render(PagerSetting setting, string urlPrefix)
        {
            return Render(setting, urlPrefix, false);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecords"></param>
        /// <param name="urlPrefix"></param>
        /// <param name="isPara">是否是参数的方式呈现如?pageno=3</param>
        /// <returns></returns>
        public static string Render(PagerSetting setting, string urlPrefix, bool isPara)
        {
            //1, 10, 120, "/"
            int DISP_COUNT = 10;//显示页码的个数，实际最后会加上“最后一页”
            int currentPage = setting.PageIndex;


            StringBuilder sb = new StringBuilder();

            sb.Append("<div class='pagination'>");
            sb.Append("<div class='pager'>");


            int totalPage = setting.RecordCount / setting.PageSize;//总页数
            if (setting.RecordCount % setting.PageSize != 0)
            {
                totalPage++;
            }

            if (totalPage == 1)
            {
                return "";
            }

            if (currentPage < 1)
            {
                currentPage = 1;
            }
            if (currentPage > totalPage)
            {
                currentPage = totalPage;
            }

            if (!urlPrefix.Contains("{0}"))
            {
                if (isPara)
                {
                    if (urlPrefix.Contains("?"))
                    {
                        urlPrefix += "&PageIndex=";
                    }
                    else
                    {
                        urlPrefix += "?PageIndex=";
                    }
                }
                else
                {
                    urlPrefix = urlPrefix.TrimEnd('/') + "/";
                }
            }

            if (setting.RecordCount > 0)
            {
                sb.Append("<span class='page_end'>共" + setting.RecordCount + "条</span>");
                //上一页
                if (currentPage == 1)
                {
                    sb.Append("<span class='page_start'>&lt;</span>");
                }
                else
                {
                    sb.Append("<a class='page_prev' href='" + GetUrl(urlPrefix, (currentPage - 1)) + "'>&lt;</a>");
                }



                //中间内容
                if (totalPage <= DISP_COUNT)
                {
                    for (int i = 1; i <= totalPage; i++)
                    {
                        if (i == currentPage)
                        {
                            sb.Append("<span class='page_cur'>" + i + "</span>");
                        }
                        else
                        {
                            sb.Append("<a href='" + GetUrl(urlPrefix, i) + "'>" + i + "</a>");
                        }
                    }
                }
                else//总页数比显示页数多
                {
                    if (currentPage <= DISP_COUNT - 4)
                    {
                        for (int i = 1; i <= DISP_COUNT - 1; i++)//因为要加上最后一页
                        {
                            if (i == currentPage)
                            {
                                sb.Append("<span class='page_cur'>" + i + "</span>");
                            }
                            else
                            {

                                sb.Append("<a href='" + GetUrl(urlPrefix, i) + "'>" + i + "</a>");
                            }
                        }

                        if (totalPage != DISP_COUNT)//如果总页数正好=DISP_COUNT就不要加...了，因为会加上最后一页
                        {
                            sb.Append("<span class='page_break'>...</span>");
                        }
                    }
                    else
                    {
                        for (int i = 1; i <= 2; i++)
                        {
                            sb.Append("<a href='" + GetUrl(urlPrefix, i) + "'>" + i + "</a>");
                        }
                        sb.Append("<span class='page_break'>...</span>");

                        int middleMax = Math.Min(currentPage + 3, totalPage - 1);//中间部分的最大值 ，由于最后一面在后面加上去，所以这里就不要了
                        int middleMin = Math.Min(currentPage - 3, middleMax - 6);//中间部分的起始值;如果到最后了，起始值要往前推
                        for (int i = middleMin; i <= middleMax; i++)
                        {
                            if (i == currentPage)
                            {
                                sb.Append("<span class='page_cur'>" + i + "</span>");
                            }
                            else
                            {
                                sb.Append("<a href='" + GetUrl(urlPrefix, i) + "'>" + i + "</a>");
                            }
                        }

                        if (middleMax < totalPage - 1)
                        {
                            sb.Append("<span class='page_break'>...</span>");
                        }

                    }
                    //加上最后一页
                    if (currentPage == totalPage)
                    {
                        sb.Append("<span class='page_cur'>" + currentPage + "</span>");
                    }
                    else
                    {
                        sb.Append("<a href='" + GetUrl(urlPrefix, totalPage) + "'>" + totalPage + "</a>");

                    }

                }


                //下一页

                if (currentPage == totalPage)
                {
                    sb.Append("<span class='page_end'>下一页&nbsp;&gt;</span>");
                }
                else
                {
                    sb.Append("<a class='page_next' href='" + GetUrl(urlPrefix, (currentPage + 1)) + "'>下一页&nbsp;&gt;</a>");
                }
            }

            sb.Append("</div>");
            sb.Append("</div>");

            return sb.ToString();
        }


        private static string GetUrl(string urlPattern, int pageIndex)
        {
            if (urlPattern.Contains("{0}"))
            {
                return string.Format(urlPattern, pageIndex);
                //return Regex.Replace(urlPattern, "@PageIndex", pageIndex.ToString(), RegexOptions.IgnoreCase);
            }
            else
            {
                return urlPattern + pageIndex;
            }
        }

        public static void SetSortImage(object button, string sortDirection)
        {
            Image imgSort = new Image();
            imgSort.ImageAlign = ImageAlign.AbsMiddle;
            if (sortDirection == "desc")
            {
                imgSort.ImageUrl = "~/common/images/common/desc.gif";
            }
            else
            {
                imgSort.ImageUrl = "~/common/images/common/asc.gif";
            }
            Literal litSpace = new Literal();
            litSpace.Text = "&nbsp;";

            Control ctl = (Control)button;
            int index = ctl.Parent.Controls.IndexOf(ctl) + 1;
            ctl.Parent.Controls.AddAt(index, litSpace);
            ctl.Parent.Controls.AddAt(index + 1, imgSort);
        }
    }
}
